package com.example.mymvvmdemo.models.add_device;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadActivity;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.ActivityAddDeviceBinding;
import com.example.mymvvmdemo.dialog.LoadingAnimDialog;
import com.example.mymvvmdemo.models.add_device.dialog.SwitchDeviceTypeDialog;
import com.example.mymvvmdemo.models.add_device.entity.DeviceTypeListEntity;
import com.example.mymvvmdemo.models.add_device.fragment.bind_device.BindDeviceFragment;
import com.example.mymvvmdemo.models.add_device.fragment.regisger_device.RegisterDeviceFragment;
import com.example.mymvvmdemo.net.presenter.GetPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class AddDeviceActivity extends BaseLoadActivity<ActivityAddDeviceBinding> implements View.OnClickListener {

    public static void startAddDeviceActivity(Activity activity) {
        Intent intent = new Intent(activity, AddDeviceActivity.class);
        activity.startActivity(intent);
    }


    private AddDeviceViewModel viewModel;
    private RegisterDeviceFragment registerDeviceFragment;
    private BindDeviceFragment bindDeviceFragment;
    /*** 加载时候的Dialog */
    private LoadingAnimDialog loadingDialog;

    @Override
    protected void showDialog() {
        if (loadingDialog == null)
            loadingDialog = new LoadingAnimDialog(this, getResources().getString(R.string.load));
        if (!loadingDialog.isShowing()) loadingDialog.show(this);
    }

    @Override
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.close(this);
        }
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(this).get(AddDeviceViewModel.class);
        initTitle(getString(R.string.add_device));
        initFragment();
    }

    @Override
    protected void initListener() {
        binding.include.ivBack.setOnClickListener(this);
        binding.clRegister.setOnClickListener(this);
        binding.clBind.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.cl_register:
                changFragment(registerDeviceFragment);
                break;
            case R.id.cl_bind:
                changFragment(bindDeviceFragment);
                break;
            default:
                break;
        }
    }


    /***********************************自定义方法**************************************/
    public void initTitle(String title) {
        binding.include.tvTitle.setVisibility(View.VISIBLE);
        binding.include.tvTitle.setText(title);
    }

    private void initFragment() {
        registerDeviceFragment = new RegisterDeviceFragment();
        registerDeviceFragment.setViewModel(viewModel);
        bindDeviceFragment = new BindDeviceFragment();
        bindDeviceFragment.setViewModel(viewModel);
        getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, registerDeviceFragment, "registerDeviceFragment").commit();
        getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, bindDeviceFragment, "bindDeviceFragment").commit();
        changFragment(registerDeviceFragment);
    }

    public void changFragment(Fragment fragment) {
        try {
            if (registerDeviceFragment == fragment) {
                getSupportFragmentManager().beginTransaction().show(registerDeviceFragment).commit();
                getSupportFragmentManager().beginTransaction().hide(bindDeviceFragment).commit();
                binding.viewRegister.setVisibility(View.VISIBLE);
                binding.viewBind.setVisibility(View.GONE);
                binding.tvRegister.setTextColor(getResources().getColor(R.color.black_two));
                binding.tvBind.setTextColor(getResources().getColor(R.color.gray_two));
            } else if (bindDeviceFragment == fragment) {
                getSupportFragmentManager().beginTransaction().hide(registerDeviceFragment).commit();
                getSupportFragmentManager().beginTransaction().show(bindDeviceFragment).commit();
                binding.viewRegister.setVisibility(View.GONE);
                binding.viewBind.setVisibility(View.VISIBLE);
                binding.tvRegister.setTextColor(getResources().getColor(R.color.gray_two));
                binding.tvBind.setTextColor(getResources().getColor(R.color.black_two));
            }
        } catch (Exception e) {
            LogUtils.e(e.getMessage());
        }
    }


}